package com.egdigital.allzic.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class PrefManager {

	protected static final String			TAG					= "PreferenceManager";						// log
	protected static final String			APP_SHARED_PREFS	= PreferenceManager.class.getSimpleName();	// Name of the file -.xml
	protected static PrefManager			mInstance;														// singleton
	protected Editor						mPrefsEditor;
	protected SharedPreferences				mSharedPrefs;
	public static HashMap<String, String>	mPreferenceDataMap;

	private PrefManager(Context context) {
		this.mSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
		this.mPrefsEditor = mSharedPrefs.edit();
	}

	public static PrefManager getInstance(Context context) {
		if (mInstance == null) {
			synchronized (PrefManager.class) {
				if (mInstance == null) {
					mInstance = new PrefManager(context);
				}
			}
		}
		return mInstance;
	}

	/**
	 * Clear the values of every shared preference
	 */
	public void clear_pref() {
		for (String value : mPreferenceDataMap.keySet()) {
			mPrefsEditor.putString(value, "");
			mPrefsEditor.commit();
		}
	}

	public void addBoolean_Pref(String key, boolean value) {
		mPrefsEditor.putBoolean(key, value);
		mPrefsEditor.commit();
	}

	public void addString_Pref(String key, String value) {
		mPrefsEditor.putString(key, value);
		mPrefsEditor.commit();
	}

	public void addInt_pref(String key, int value) {
		mPrefsEditor.putInt(key, value);
		mPrefsEditor.commit();
	}

	public void addLong_pref(String key, long value) {
		mPrefsEditor.putLong(key, value);
		mPrefsEditor.commit();
	}

	public Exception addString_Prefs(List<String> key, List<String> values) {
		for (int i = 0; i < key.size(); i++) {
			try {
				mPrefsEditor.putString(key.get(i), values.get(i));
				mPrefsEditor.commit();
			} catch (Exception e) {
				return e;
			}
		}
		return null;
	}

	/**
	 * @param map
	 *        {@link HashMap} of (key,value) datas
	 * @throws Exception
	 *         any exception
	 */
	public void addStringMap_prefs(HashMap<String, String> map) throws Exception {
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry pairs = (Map.Entry) it.next();
			// add it to hashmap
			mPrefsEditor.putString(pairs.getKey().toString(), pairs.getValue().toString());
			mPrefsEditor.commit();
			it.remove(); // avoids a ConcurrentModificationException
		}
	}

	public Boolean getBoolean_prefForKey(String key) {
		return mSharedPrefs.getBoolean(key, true);
	}

	public String getString_prefForKey(String key) {
		return mSharedPrefs.getString(key, "");
	}

	public int getInt_prefForKey(String key) {
		return mSharedPrefs.getInt(key, 0);
	}

	public long getLong_prefForKey(String key) {
		return mSharedPrefs.getLong(key, 0);
	}
}
